package com.ctosb.classviewer.clazz.attr;

import com.ctosb.classviewer.ClassReader;
import com.ctosb.classviewer.clazz.AbstractParse;
import com.ctosb.classviewer.clazz.cp.CONSTANT_Class_info;
import com.ctosb.classviewer.clazz.cp.CONSTANT_Utf8_info;
import lombok.Data;
import lombok.experimental.Accessors;

@Data
@Accessors(chain = true)
public class InnerClass extends AbstractParse {

    private short inner_class_info_index;
    private short outer_class_info_index;
    private short inner_name_index;
    private short inner_class_access_flags;

    @Override
    public InnerClass parse(ClassReader classReader) {
        this.inner_class_info_index = classReader.readShort();
        this.outer_class_info_index = classReader.readShort();
        this.inner_name_index = classReader.readShort();
        this.inner_class_access_flags = classReader.readShort();
        return this;
    }

    public CONSTANT_Class_info getInner_class_info() {
        return (CONSTANT_Class_info) getClassFile().getConstant_pool()[inner_class_info_index];
    }

    public CONSTANT_Class_info getOuter_class_info() {
        return (CONSTANT_Class_info) getClassFile().getConstant_pool()[outer_class_info_index];
    }

    public String getInner_name() {
        if(inner_name_index == 0) {
            return null;
        }
        return ((CONSTANT_Utf8_info) getClassFile().getConstant_pool()[inner_name_index]).getValue();
    }
}
